package com.efesco.inchar.list;

import java.util.Date;
import java.util.Calendar;

import com.bstek.dorado.data.Dataset;
import com.bstek.dorado.data.ParameterSet;
import com.bstek.dorado.data.Record;
import com.bstek.dorado.data.RecordIterator;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.utils.variant.VariantSet;
import com.bstek.dorado.view.DefaultViewModel;
import com.bstek.dorado.view.data.ViewDataset;


/**
 * ClassifyViewModel
 */
public class ReClassifyViewModel extends DefaultViewModel {

  public void doClassify(ParameterSet parameters)
      throws Exception {
    Dataset datasetClassifyApplyDate = getDataset("datasetClassifyApplyDate");
    Dataset datasetClassify = getDataset("datasetClassify");
    Dataset datasetDetail = getDataset("datasetDetail");
    long typeId = datasetDetail.getLong("IN_CHAR_EMPLOYEE_TYPE2_ID");
    String typeName = datasetDetail.getString("EMPLOYEE_TYPE_NAME");
    datasetClassifyApplyDate.moveFirst();
    Date caDate = datasetClassifyApplyDate.getDate("CLASSIFY_APPLY_DATE");
    Calendar ca = Calendar.getInstance();
    ca.setTime(caDate);
    String classifyApplyDate = "" + ca.get(Calendar.YEAR) + "-" + (ca.get(Calendar.MONTH) + 1) + "-" + ca.get(Calendar.DATE);
  
    long oldInCharId = datasetClassify.getLong("IN_CHAR_ID");
    long newInCharId = insertInCharList(oldInCharId, classifyApplyDate, typeId);
    addServiceTransaction(newInCharId);
    //addMaterialReceive(newInCharId);   
    
    keepServiceTransaction(newInCharId);
    keepMaterialReceive(newInCharId);
    datasetClassify.setLong("IN_CHAR_ID", newInCharId);
    datasetClassify.setString("EMPLOYEE_TYPE_NAME", typeName);
    datasetClassify.setString("STATUS", "2");
    disableInCharRecord(oldInCharId);
    
    DBStatement stmt = new DBStatement();
    stmt.setSql("update s_in_char_list icl set icl.status='9' where icl.in_char_id="+newInCharId+" "
       +"and not exists(select 's' from s_in_char_service_transaction where icl.in_char_id=in_char_id and status=1)");
    stmt.execute();
    stmt.close();
  }
  
  private long insertInCharList(long argOldInCharId, String argClassifyApplyDate, long argTypeId) throws Exception {
    DBStatement st = new DBStatement();
    st.setSql("SELECT GENERAL.Nextval FROM DUAL");
    VariantSet varSet = st.query();
    long newInCharId = varSet.getLong(0);
    
    DBStatement stmt = new DBStatement();
    try {
      stmt.setSql("INSERT INTO S_IN_CHAR_LIST(IN_CHAR_ID, EMPLOYEE_ID, EMPLOYEE_TYPE, STATUS, CREATE_DATE, UPDATE_DATE, CLASSIFY_DATE, EMPLOYEE_ON_OFF_ID, " +
      "CLASSIFY_APPLY_DATE,if_notice,notice_date,if_has_exam,if_take_exam,exam_date,exam_result,process_ins_id) " + 
      "SELECT " + newInCharId + ", EMPLOYEE_ID, " + argTypeId + ", 2, SYSDATE, SYSDATE, SYSDATE, EMPLOYEE_ON_OFF_ID, TO_DATE('" + argClassifyApplyDate + 
      "', 'YYYY-MM-DD'),if_notice,notice_date,if_has_exam,if_take_exam,exam_date,exam_result,process_ins_id FROM S_IN_CHAR_LIST WHERE IN_CHAR_ID = " + argOldInCharId);
      stmt.execute();
    }
    finally {
      stmt.close();
    }
    
    return newInCharId;

  }
  
  private void disableInCharRecord(long argInCharId) throws Exception {
    DBStatement stmt = new DBStatement();
    try {
      stmt.setSql("UPDATE S_IN_CHAR_LIST SET STATUS = '0' WHERE IN_CHAR_ID = " + argInCharId);
      stmt.execute();
    }
    finally {
      stmt.close();
    }

  }
  
  private void keepServiceTransaction(long argInCharId) throws Exception {
    Dataset datasetFinishedService = getDataset("datasetFinishedService");
    
    RecordIterator iter = datasetFinishedService.recordIterator();
    while (iter.hasNext()) {
      Record record = iter.nextRecord();
      long serviceId = record.getLong("IN_CHAR_SERVICE_ID");
      updateServiceTransaction(argInCharId, serviceId);
    }

  }
  
  private void updateServiceTransaction(long argInCharId, long argInCharServiceId) throws Exception {
    DBStatement stmt = new DBStatement();
    try {
      stmt.setSql("UPDATE S_IN_CHAR_SERVICE_TRANSACTION SET STATUS = '2' WHERE IN_CHAR_ID = " + argInCharId + 
      " AND IN_CHAR_SERVICE_ID = " + argInCharServiceId);
      stmt.execute();
    }
    finally {
      stmt.close();
    }

  }
  
  private void keepMaterialReceive(long argInCharId) throws Exception {
    Dataset datasetReceivedMaterial = getDataset("datasetReceivedMaterial");
    
    RecordIterator iter = datasetReceivedMaterial.recordIterator();
    while (iter.hasNext()) {
      Record record = iter.nextRecord();
      long materialId = record.getLong("MATERIAL_ID");
      updateMaterialReceive(argInCharId, materialId);
    }

  }
  
  private void updateMaterialReceive(long argInCharId, long argMaterialId) throws Exception {
    DBStatement stmt = new DBStatement();
    try {
      stmt.setSql("UPDATE S_IN_CHAR_MATERIAL_RECEIVE SET STATUS = '2' WHERE IN_CHAR_ID = " + argInCharId + 
      " AND MATERIAL_ID = " + argMaterialId);
      stmt.execute();
    }
    finally {
      stmt.close();
    }

  }
  
  private void addServiceTransaction(long argInCharId) throws Exception {
    Dataset datasetTypeService = getDataset("datasetTypeService");
    
    //RecordIterator iter = datasetTypeService.recordIterator();
    datasetTypeService.moveFirst();
    while (!datasetTypeService.isLast()) {
      Record record = datasetTypeService.getCurrent();
      long serviceId = record.getLong("IN_CHAR_SERVICE_ID");
      String serviceType=record.getString("SERVICE_TYPE");
      insertServiceTransaction(argInCharId, serviceId,serviceType);
      
      addMaterialReceive(argInCharId);   
      
      datasetTypeService.moveNext();
    }

  }
  
  private void insertServiceTransaction(long argInCharId, long argServiceId,String serviceType) throws Exception {
    DBStatement stmt = new DBStatement();
    try {
    	String status="2";
        //33,1,10,30,20,11,13,12
        if (serviceType.equals("33")||serviceType.equals("1")||serviceType.equals("10")||serviceType.equals("30")||
      	  serviceType.equals("20")||serviceType.equals("11")||serviceType.equals("13")||serviceType.equals("12"))
        {
      	  status="1";
        }
	    stmt.setSql("INSERT INTO S_IN_CHAR_SERVICE_TRANSACTION " + 
	      "(IN_CHAR_SERVICE_TRANSACTION_ID, IN_CHAR_ID, IN_CHAR_SERVICE_ID, CREATE_DATE, UPDATE_DATE, STATUS) " +
	      "VALUES (GENERAL.NEXTVAL, " + argInCharId + ", " + argServiceId + ", SYSDATE, SYSDATE, '"+status+"')");
	    stmt.execute();
    }
    finally {
      stmt.close();
    }

  }
  
  private void addMaterialReceive(long argInCharId) throws Exception {
    Dataset datasetServiceMaterial = getDataset("datasetServiceMaterial");
    
    //RecordIterator iter = datasetServiceMaterial.recordIterator();
    datasetServiceMaterial.moveFirst();
    while (!datasetServiceMaterial.isLast()) {
      Record record = datasetServiceMaterial.getCurrent();
      long materialId = record.getLong("MATERIAL_ID");
      insertMaterialReceive(argInCharId, materialId);
      
      datasetServiceMaterial.moveNext();
    }

  }
  
  private void insertMaterialReceive(long argInCharId, long argMaterialId) throws Exception {
    DBStatement stmt = new DBStatement();
    try {
      stmt.setSql("INSERT INTO S_IN_CHAR_MATERIAL_RECEIVE " + 
      "(IN_CHAR_MATERIAL_RECEIVE_ID, IN_CHAR_ID, MATERIAL_ID, CREATE_DATE, UPDATE_DATE, STATUS) " +
      "VALUES (GENERAL.NEXTVAL, " + argInCharId + ", " + argMaterialId + ", SYSDATE, SYSDATE, '1')");
      stmt.execute();
    }
    finally {
      stmt.close();
    }

  }

}
